/*
 * Copyright 2002-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.core.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

import org.springframework.lang.Nullable;

/**
 * 抽象底层的真实资源文件(比如普通文件和类加载路径下的资源文件)形成的资源文件描述性质的接口.
 * 表面上看,如果一个文件是在物理上真实存在的,能够为每一个资源打开一个输入流,不过事实上,
 * URL和文件句柄通常只能返回部分资源,因为这些实际的行为是依赖于特定的实现的.
 * <p>
 * <p>
 * <p>
 * Interface for a resource descriptor that abstracts from the actual
 * type of underlying resource, such as a file or class path resource.
 * <p>
 * <p>An InputStream can be opened for every resource if it exists in
 * physical form, but a URL or File handle can just be returned for
 * certain resources. The actual behavior is implementation-specific.
 *
 * @author Juergen Hoeller
 * @see #getInputStream()
 * @see #getURL()
 * @see #getURI()
 * @see #getFile()
 * @see WritableResource
 * @see ContextResource
 * @see UrlResource
 * @see ClassPathResource
 * @see FileSystemResource
 * @see PathResource
 * @see ByteArrayResource
 * @see InputStreamResource
 * @since 28.12.2003
 */
public interface Resource extends InputStreamSource {
	/**
	 * Resource继承了InputStreamSource接口,但是它并没有实现InputStreamSource接口的唯一方法getInputStream,
	 * 而是扩展了对于资源文件的一些基础定义和描述.
	 */


	/**
	 * [存在性]
	 * 确定这个资源在物理形式上是否真正存在.
	 * 这个方法本质上是执行一次明确的检查,,不过这次检查仅仅能够保证他是一个有效的描述符句柄.
	 * <p>
	 * <p>
	 * Determine whether this resource actually exists in physical form.
	 * <p>This method performs a definitive existence check, whereas the
	 * existence of a {@code Resource} handle only guarantees a valid
	 * descriptor handle.
	 */
	boolean exists();

	/**
	 * [可读性]
	 * 表名当前资源文件能否通过getInputStream方法读取.
	 * 如果他是典型的资源描述符,他就会返回true.
	 * 但是,需要注意的是,事实上,在真正读取的时候,他依然有可能失败.
	 * 不过呢,当他返回false的时候,那就一定表示当前资源文件内容不能够被读取.
	 * <p>
	 * <p>
	 * Indicate whether the contents of this resource can be read via
	 * {@link #getInputStream()}.
	 * <p>Will be {@code true} for typical resource descriptors;
	 * note that actual content reading may still fail when attempted.
	 * However, a value of {@code false} is a definitive indication
	 * that the resource content cannot be read.
	 *
	 * @see #getInputStream()
	 */
	default boolean isReadable() {
		return true;
	}

	/**
	 * [是否处于打开状态]
	 * 表明当前资源是否表现出了一个正在打开流的句柄.
	 * 如果返回true,那么这个流就不能被多次读取,而且,还必须被读取和关闭,避免资源泄漏.
	 * 不过,对于典型的资源描述符将会返回false.
	 * <p>
	 * <p>
	 * Indicate whether this resource represents a handle with an open stream.
	 * If {@code true}, the InputStream cannot be read multiple times,
	 * and must be read and closed to avoid resource leaks.
	 * <p>Will be {@code false} for typical resource descriptors.
	 */
	default boolean isOpen() {
		return false;
	}

	/**
	 * 确定当前资源是不是一个存在与文件系统内的文件.
	 * 如果返回了true,那么表示调用getFile方法的时候能够成功,但是不能保证一定成功.
	 * 方法默认为false(保守).
	 * <p>
	 * <p>
	 * Determine whether this resource represents a file in a file system.
	 * A value of {@code true} strongly suggests (but does not guarantee)
	 * that a {@link #getFile()} call will succeed.
	 * <p>This is conservatively {@code false} by default.
	 *
	 * @see #getFile()
	 * @since 5.0
	 */
	default boolean isFile() {
		return false;
	}

	/**
	 * 返回当前资源的URL句柄
	 * <p>
	 * <p>
	 * Return a URL handle for this resource.
	 *
	 * @throws IOException if the resource cannot be resolved as URL,
	 *                     i.e. if the resource is not available as descriptor
	 */
	URL getURL() throws IOException;

	/**
	 * 返回当前资源文件的URI句柄.
	 * <p>
	 * <p>
	 * Return a URI handle for this resource.
	 *
	 * @throws IOException if the resource cannot be resolved as URI,
	 *                     i.e. if the resource is not available as descriptor
	 * @since 2.5
	 */
	URI getURI() throws IOException;

	/**
	 * 返回当前资源的文件句柄
	 * <p>
	 * <p>
	 * Return a File handle for this resource.
	 *
	 * @throws java.io.FileNotFoundException if the resource cannot be resolved as
	 *                                       absolute file path, i.e. if the resource is not available in a file system
	 * @throws IOException                   in case of general resolution/reading failures
	 * @see #getInputStream()
	 */
	File getFile() throws IOException;

	/**
	 * 返回一个{@link ReadableByteChannel]}.[ReadableByteChannel主要提供将Buffer转换成bytes序列.]
	 * 预期,每一次调用都会返回一个新的channel.
	 * 默认实现会返回一个带有{@link #getInputStream()}.结果的{@link Channels},
	 * <p>
	 * <p>
	 * Return a {@link ReadableByteChannel}.
	 * <p>It is expected that each call creates a <i>fresh</i> channel.
	 * <p>The default implementation returns {@link Channels#newChannel(InputStream)}
	 * with the result of {@link #getInputStream()}.
	 *
	 * @return the byte channel for the underlying resource (must not be {@code null})
	 * @throws java.io.FileNotFoundException if the underlying resource doesn't exist
	 * @throws IOException                   if the content channel could not be opened
	 * @see #getInputStream()
	 * @since 5.0
	 */
	default ReadableByteChannel readableChannel() throws IOException {
		return Channels.newChannel(getInputStream());
	}

	/**
	 * 表名当前资源的内容长度.
	 * <p>
	 * <p>
	 * Determine the content length for this resource.
	 *
	 * @throws IOException if the resource cannot be resolved
	 *                     (in the file system or as some other known physical resource type)
	 */
	long contentLength() throws IOException;

	/**
	 * 表名当前资源最后一次修改的时间.
	 * Determine the last-modified timestamp for this resource.
	 *
	 * @throws IOException if the resource cannot be resolved
	 *                     (in the file system or as some other known physical resource type)
	 */
	long lastModified() throws IOException;

	/**
	 * 相对于当前资源创建一个新的资源.
	 * Create a resource relative to this resource.
	 *
	 * @param relativePath the relative path (relative to this resource) 相对于当前资源的相对路径.
	 * @return the resource handle for the relative resource 相对于当前资源的资源句柄.
	 * @throws IOException if the relative resource cannot be determined
	 */
	Resource createRelative(String relativePath) throws IOException;

	/**
	 * 表名当前资源的文件名,通常是路径的最后一部分,比如:"myfile.txt"
	 * 如果当前资源没有名字,将会返回null.
	 * <p>
	 * <p>
	 * Determine a filename for this resource, i.e. typically the last
	 * part of the path: for example, "myfile.txt".
	 * <p>Returns {@code null} if this type of resource does not
	 * have a filename.
	 */
	@Nullable
	String getFilename();

	/**
	 * 返回当前资源的描述符,用于处理资源时的错误输出.
	 * 比较建议通过toString方法来实现该方法的功能.
	 * <p>
	 * <p>
	 * Return a description for this resource,
	 * to be used for error output when working with the resource.
	 * <p>Implementations are also encouraged to return this value
	 * from their {@code toString} method.
	 *
	 * @see Object#toString()
	 */
	String getDescription();

}
